﻿using AR.Entities;
using AR.Web.Services;
using System;
using System.ComponentModel;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;

namespace AR.Models.Parametrage
{
    public class ThemeModel
    {
        #region Contexts
        public EntretienTechniqueContext EntretientechniqueContext;
        public CandidatContext candidatsContext; 
        #endregion

        #region Themes
        public ThemeModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                EntretientechniqueContext = new EntretienTechniqueContext();
                candidatsContext = new CandidatContext();
            }
        }
        public EntitySet<THEME> GetThemes()
        {
            var query = EntretientechniqueContext.GetTHEMEQuery();
            EntretientechniqueContext.Load(query);
            return EntretientechniqueContext.THEMEs;
        }
        public bool UpdateTheme(int id, string intitule)
        {
            EntretientechniqueContext.THEMEs.Where(x => x.ID_THEME == id).First().THEME_INTITULE = intitule;
            EntretientechniqueContext.SubmitChanges();
            return true;
        }
        public bool InsertTheme(string intituleTheme)
        {
            THEME ntheme = new THEME();
            ntheme.THEME_INTITULE = intituleTheme;
            EntretientechniqueContext.THEMEs.Add(ntheme);
            EntretientechniqueContext.SubmitChanges();
            return true;
        }
        public bool RemoveTheme(THEME rtheme)
        {
            EntretientechniqueContext.THEMEs.Remove(rtheme);
            EntretientechniqueContext.SubmitChanges();
            return true;
        }         
        #endregion
        #region Questions
        public EntitySet<QUESTIONS> GetQUESTIONS()
        {
            var query = EntretientechniqueContext.GetQUESTIONSQuery();
            EntretientechniqueContext.Load(query);
            return EntretientechniqueContext.QUESTIONS;
        }
        public bool UpdateQuestion(int id, string intitule)
        {
            EntretientechniqueContext.QUESTIONS.Where(x => x.ID_QUESTION == id).First().QUESTION = intitule;
            EntretientechniqueContext.SubmitChanges();
            return true;
        }
        public bool InsertQuestion(int id, string intituleQuestion)
        {
            QUESTIONS nQuestion = new QUESTIONS();
            nQuestion.QUESTION = intituleQuestion;
            nQuestion.ID_THEME = id;
            EntretientechniqueContext.QUESTIONS.Add(nQuestion);
            EntretientechniqueContext.SubmitChanges();
            return true;
        }
        public bool RemoveQuestion(QUESTIONS rQuestion)
        {
            EntretientechniqueContext.QUESTIONS.Remove(rQuestion);
            EntretientechniqueContext.SubmitChanges();
            return true;
        } 
        #endregion
        #region Choix
        public EntitySet<CHOIX> GetCHOIX()
        {
            var query = EntretientechniqueContext.GetCHOIXQuery();
            EntretientechniqueContext.Load(query);
            return EntretientechniqueContext.CHOIXes;
        }
        public bool UpdateChoix(int id, string intitule, bool isCorrect)
        {
            EntretientechniqueContext.CHOIXes.Where(x => x.IDCHOIX == id).First().CHOIX1 = intitule;
            EntretientechniqueContext.CHOIXes.Where(x => x.IDCHOIX == id).First().IsCorrect = isCorrect;
            EntretientechniqueContext.SubmitChanges();
            return true;
        }
        public bool InsertChoix(string intituleChoix, bool isChoixCorrect, int IdQuestion)
        {
            CHOIX nChoix = new CHOIX();
            nChoix.CHOIX1 = intituleChoix;
            nChoix.IsCorrect = isChoixCorrect;
            nChoix.ID_QUESTION = IdQuestion;
            EntretientechniqueContext.CHOIXes.Add(nChoix);
            EntretientechniqueContext.SubmitChanges();
            return true;
        }
        public bool RemoveChoix(CHOIX rChoix)
        {
            EntretientechniqueContext.CHOIXes.Remove(rChoix);
            EntretientechniqueContext.SubmitChanges();
            return true;
        } 
        #endregion
        #region Practices
        public EntitySet<PRACTICE> GetPractices()
        {
            var query = EntretientechniqueContext.GetPRACTICEQuery();
            EntretientechniqueContext.Load(query);
            return EntretientechniqueContext.PRACTICEs;
        }
        public bool UpdatePractice(int id, string intitule)
        {
            EntretientechniqueContext.PRACTICEs.Where(x => x.ID_PRACTICE == id).First().PRACTICE1 = intitule;
            EntretientechniqueContext.SubmitChanges();
            return true;
        }
        public bool InsertPractice(string intitulee)
        {
            PRACTICE npractice = new PRACTICE();
            npractice.PRACTICE1 = intitulee;
            EntretientechniqueContext.PRACTICEs.Add(npractice);
            EntretientechniqueContext.SubmitChanges();
            return true;
        }
        public bool RemovePractice(PRACTICE rpractice)
        {
            EntretientechniqueContext.PRACTICEs.Remove(rpractice);
            EntretientechniqueContext.SubmitChanges();
            return true;
        }
        public int NombreCandidatsUtilisantPractice(int id)
        {
            return EntretientechniqueContext.CANDIDATs.Where(c => c.ID_PRACTICE == id).Count();
        }
        public int NombreTestsUtilisantPractice(int id)
        {
            return EntretientechniqueContext.TEST_TECHNIQUEs.Where(t => t.ID_PRACTICE == id).Count();
        } 
        #endregion
        #region Tests
        public EntitySet<TEST_TECHNIQUE> GetTests()
        {
            var query = EntretientechniqueContext.GetTEST_TECHNIQUEsQuery();
            EntretientechniqueContext.Load(query);
            return EntretientechniqueContext.TEST_TECHNIQUEs;
        }
        public bool UpdateTest(int id, string intitule)
        {
            EntretientechniqueContext.TEST_TECHNIQUEs.Where(x => x.ID_TEST == id).First().TEST_INTITULE = intitule;
            EntretientechniqueContext.SubmitChanges();
            return true;
        }
        public bool InsertTest(int idPractice, string intitulee)
        {
            TEST_TECHNIQUE nTest = new TEST_TECHNIQUE();
            nTest.TEST_INTITULE = intitulee;
            nTest.ID_PRACTICE = idPractice;
            EntretientechniqueContext.TEST_TECHNIQUEs.Add(nTest);
            EntretientechniqueContext.SubmitChanges();
            return true;
        }
        public bool RemoveTest(TEST_TECHNIQUE rTest)
        {
            EntretientechniqueContext.TEST_TECHNIQUEs.Remove(rTest);
            EntretientechniqueContext.SubmitChanges();
            return true;
        } 
        #endregion
        #region ThemesTest
        public bool AjoutThemeTest(int idTest, int idTheme)
        {
            THEMES__TEST_TECHNIQUE nTestTheme = new THEMES__TEST_TECHNIQUE();
            nTestTheme.ID_TEST = idTest;
            nTestTheme.ID_THEME = idTheme;
            TEST_TECHNIQUE testTechniqueTrouve = EntretientechniqueContext.TEST_TECHNIQUEs.Where(w => w.ID_TEST == idTest).First();
            testTechniqueTrouve.THEMES__TEST_TECHNIQUE.Add(nTestTheme);
            EntretientechniqueContext.SubmitChanges();
            return true;
        }
        public bool SupressionThemeTest(int idTest, int idTheme)
        {
            var query = EntretientechniqueContext.GetTHEMES__TEST_TECHNIQUEQuery();
            EntretientechniqueContext.Load<THEMES__TEST_TECHNIQUE>(query).Completed += (sender, args) =>
            {
                THEMES__TEST_TECHNIQUE rTestTheme = EntretientechniqueContext.THEMES__TEST_TECHNIQUEs.Where(w => w.ID_TEST == idTest && w.ID_THEME == idTheme).First();
                EntretientechniqueContext.THEMES__TEST_TECHNIQUEs.Remove(rTestTheme);
                EntretientechniqueContext.SubmitChanges();
            };
            return true;
        }
        public int NombreTestsUtilisantTheme(int id)
        {
            return EntretientechniqueContext.THEMES__TEST_TECHNIQUEs.Where(w => w.ID_THEME == id).Count();
        } 
        #endregion
        #region Etudes
        internal EntitySet<NIVEAU_ETUDE> GetEtudes()
        {
            var query = candidatsContext.GetNIVEAU_ETUDEQuery();
            candidatsContext.Load(query);
            return candidatsContext.NIVEAU_ETUDEs;
        }
        public bool UpdateEtude(int id, string intitule)
        {
            candidatsContext.NIVEAU_ETUDEs.Where(x => x.ID_ETUDE == id).First().INTITULE = intitule;
            candidatsContext.SubmitChanges();
            return true;
        }
        public bool InsertEtude(string intitulee)
        {
            NIVEAU_ETUDE netude = new NIVEAU_ETUDE();
            netude.INTITULE = intitulee;
            candidatsContext.NIVEAU_ETUDEs.Add(netude);
            candidatsContext.SubmitChanges();
            return true;
        }
        public bool RemoveEtude(NIVEAU_ETUDE retude)
        {
            candidatsContext.NIVEAU_ETUDEs.Remove(retude);
            candidatsContext.SubmitChanges();
            return true;
        }
        public int NombreCandidatsUtilisantEtude(int id)
        {
            return EntretientechniqueContext.CANDIDATs.Where(c => c.ID_ETUDE == id).Count();
        }
        #endregion
        #region Experiences
        internal EntitySet<NIVEAU_EXPERIENCE> GetExperiences()
        {
            var query = candidatsContext.GetNIVEAU_EXPERIENCEQuery();
            candidatsContext.Load(query);
            return candidatsContext.NIVEAU_EXPERIENCEs;
        }
        public bool UpdateExperience(int id, string intitule)
        {
            candidatsContext.NIVEAU_EXPERIENCEs.Where(x => x.ID_EXPERIENCE == id).First().INTITULE = intitule;
            candidatsContext.SubmitChanges();
            return true;
        }
        public bool InsertExperience(string intitulee)
        {
            NIVEAU_EXPERIENCE nexperience = new NIVEAU_EXPERIENCE();
            nexperience.INTITULE = intitulee;
            candidatsContext.NIVEAU_EXPERIENCEs.Add(nexperience);
            candidatsContext.SubmitChanges();
            return true;
        }
        public bool RemoveExperience(NIVEAU_EXPERIENCE rexperience)
        {
            candidatsContext.NIVEAU_EXPERIENCEs.Remove(rexperience);
            candidatsContext.SubmitChanges();
            return true;
        }
        public int NombreCandidatsUtilisantExperience(int id)
        {
            return EntretientechniqueContext.CANDIDATs.Where(c => c.ID_EXPERIENCE == id).Count();
        }
        #endregion
        #region Projets
        internal EntitySet<PROJET> GetProjets()
        {
            var query = candidatsContext.GetPROJETQuery();
            candidatsContext.Load(query);
            return candidatsContext.PROJETs;
        }
        public bool UpdateProjet(int id, string intitule)
        {
            candidatsContext.PROJETs.Where(x => x.IDPROJET == id).First().PROJET1 = intitule;
            candidatsContext.SubmitChanges();
            return true;
        }
        public bool InsertProjet(string intitulee)
        {
            PROJET nprojet = new PROJET();
            nprojet.PROJET1 = intitulee;
            candidatsContext.PROJETs.Add(nprojet);
            candidatsContext.SubmitChanges();
            return true;
        }
        public bool RemoveProjet(PROJET rprojet)
        {
            candidatsContext.PROJETs.Remove(rprojet);
            candidatsContext.SubmitChanges();
            return true;
        }
        public int NombreCandidatsUtilisantProjet(int id)
        {
            return EntretientechniqueContext.CANDIDATs.Where(c => c.ID_PROJET == id).Count();
        }
        #endregion
        #region Postes
        internal EntitySet<POSTE> GetPostes()
        {
            var query = candidatsContext.GetPOSTEQuery();
            candidatsContext.Load(query);
            return candidatsContext.POSTEs;
        }
        public bool UpdatePoste(int id, string intitule)
        {
            candidatsContext.POSTEs.Where(x => x.IDPOSTE == id).First().POST = intitule;
            candidatsContext.SubmitChanges();
            return true;
        }
        public bool InsertPoste(string intitulee)
        {
            POSTE nposte = new POSTE();
            nposte.POST = intitulee;
            candidatsContext.POSTEs.Add(nposte);
            candidatsContext.SubmitChanges();
            return true;
        }
        public bool RemovePoste(POSTE rposte)
        {
            candidatsContext.POSTEs.Remove(rposte);
            candidatsContext.SubmitChanges();
            return true;
        }
        public int NombreCandidatsUtilisantPoste(int id)
        {
            return EntretientechniqueContext.CANDIDATs.Where(c => c.ID_POSTE == id).Count();
        }
        #endregion

        internal EntitySet<CANDIDAT> GetCandidats()
        {
            var query = candidatsContext.GetCANDIDATQuery();
            candidatsContext.Load(query);
            return candidatsContext.CANDIDATs;
        }
    }
}
